package com.lc.YYGH.hosp.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lc.YYGH.common.result.Result;
import com.lc.YYGH.common.utils.MD5;
import com.lc.YYGH.hosp.service.HospitalSetService;
import com.lc.YYGH.model.hosp.HospitalSet;
import com.lc.YYGH.vo.hosp.HospitalSetQueryVo;
import com.mysql.jdbc.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Random;
//针对医院设置的crud方法如下：
@Api(tags="医院设置管理")
//swagger2 注解
// @Api:修饰整个类，描述Controller的作用
//@ApiOperation:描述Controller类的一个方法，或者说一个借口
//@ApiParam:单个参数描述
//@ApiModel:用对象来接收参数
//@ApiModelProperty:用对象接收参数时，描述对象的一个属性
@RestController
@RequestMapping("/admin/hosp/hospitalSet")
@CrossOrigin
public class HospitalSetController {
    @Autowired
    private HospitalSetService hospitalSetService;
    //获取医院设置列表
    @GetMapping("findAll")
    @ApiOperation(value="获取医院设置列表")
    public Result findAll()
    {
        List<HospitalSet> list=hospitalSetService.list();
        return Result.success(list);

    }
    //2 逻辑删除医院设置
    @DeleteMapping("{id}")
    @ApiOperation(value="逻辑删除医院设置")
    public Result deleteById(@PathVariable Long id)
    {
        boolean b=hospitalSetService.removeById(id);
        if(b)
        {
            return Result.success();
        }
        return Result.fail();
    }

    //条件查询带分页
    @ApiOperation(value="条件查询带分页")
    @PostMapping("findPageHospSet/{current}/{limit}")
    public Result findPageHospset(@PathVariable long current,
                                  @PathVariable long limit,
                                  @RequestBody(required = false) HospitalSetQueryVo hospitalSetQueryVo)
    {
        Page<HospitalSet> page=new Page<>(current,limit);
        QueryWrapper<HospitalSet> wrapper=new QueryWrapper<>();
        String hosname = null;
        String hoscode = null;
        if(hospitalSetQueryVo != null){
            hosname=hospitalSetQueryVo.getHosname();
            hoscode=hospitalSetQueryVo.getHoscode();

        }
        if(!StringUtils.isNullOrEmpty(hosname))
        {
            wrapper.like("hosname",hosname);  //精确查询
        }
        if(!StringUtils.isNullOrEmpty(hoscode))
        {
            wrapper.eq("hoscode",hoscode);  //模糊查询
        }
        Page<HospitalSet> pageHospitalSet=hospitalSetService.page(page,wrapper);
        return Result.success(pageHospitalSet);

    }

    //添加医院设置
    @PostMapping("saveHospitalSet")
    @ApiOperation(value="添加医院设置")

    public Result saveHospitalSet(@RequestBody HospitalSet hospitalSet)
    {
        //设置医院状态（1 正常使用 0 锁定不能使用）

        hospitalSet.setStatus(1);
        Random random=new Random();
        String src=System.currentTimeMillis()+""+random.nextInt(1000);
                String encrypt=MD5.encrypt(src);
        hospitalSet.setSignKey(encrypt);
        boolean b=hospitalSetService.save(hospitalSet);
        if(b)
        {
            return Result.success();
        }
        else
        {
            return Result.fail();
        }

    }
    //根据id获取医院设置
    @ApiOperation(value="根据id获取医院设置")
    @GetMapping("getHospSet/{id}")
    public Result getHospSet(@PathVariable Long id)
    {
        HospitalSet hospitalSet=hospitalSetService.getById(id);
        return Result.success(hospitalSet);

    }
    //修改医院设置
    @ApiOperation(value="修改医院设置")
    @PutMapping  ("updateHospitalSet")
    public Result updateHospitalSet(@RequestBody HospitalSet hospitalSet)
    {
        boolean flag=hospitalSetService.updateById(hospitalSet);
        if(flag)
        {
            return Result.success();
        }
        else
        {
            return Result.fail();
        }
    }
    //批量删除医院设置
    @ApiOperation(value="批量删除医院设置")
    @DeleteMapping("batchRemove")
    public Result batchRemoveHospitalSet(@RequestBody List<Long> idList)
    {
        hospitalSetService.removeByIds(idList);
        return Result.success();
    }
    //医院锁定及解锁
    @PutMapping("lockHospitalSet/{id}/{status}")
    @ApiOperation("医院锁定以及解锁")
    public Result lockHospSet(@PathVariable Long id,@PathVariable Integer status)
    {
        HospitalSet hospitalSet=hospitalSetService.getById(id);
        hospitalSet.setStatus(status);
        hospitalSetService.updateById(hospitalSet);
        return Result.success();

    }
    //发送签名Key 医院信息配置后，可以通过短信的形式发送医院编号与签名key给联系人，
    // 联系人拿到该信息就可以参考
    @PutMapping("sendKey/{id}")
            public Result sendKey(@PathVariable Long id)
    {
        HospitalSet hospitalSet=hospitalSetService.getById(id);
        String signKey=hospitalSet.getSignKey();
        String hoscode=hospitalSet.getHoscode();
        //TODO 发送短信（未完成，待办事项）
        return Result.success();




    }




}
